using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using Myotragus.Data.Emit;
using Myotragus.Data.Util;
using NUnit.Framework;
using System.Linq;

namespace Myotragus.Data.Tests.Tests.Emit
{
	[TestFixture]
	public class DynamicTypeFixture
	{
		[TestFixtureSetUp]
		public void SetUp()
		{
			NameGenerator = new GuidNameGenerator();
			var name = NameGenerator.CreateAssemblyName();

			Assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
				new AssemblyName(name), AssemblyBuilderAccess.Run);
			Module = Assembly.DefineDynamicModule(name + ".dll");
		}

		protected ModuleBuilder Module { get; set; }
		protected AssemblyBuilder Assembly { get; set; }
		protected GuidNameGenerator NameGenerator { get; set; }

		[Test]
		[TestCase("Type1", TypeAttributes.Class, typeof(object), new Type[0])]
		public void CreateType(string name, TypeAttributes attr, Type baseType, 
			IEnumerable<Type> interfaces)
		{
			var dyn = new DynamicType(NameGenerator, Module, name, 
				attr, baseType, interfaces);

			var type = dyn.CreateType();

			Assert.AreEqual(type.Name, name);
			Assert.AreEqual(type.BaseType, baseType);
			Assert.IsTrue(!type.GetInterfaces().Except(interfaces).Any());
		}
	}
}